package com.itbaizhan;
/*设置响应类型*/

/*HttpServletResponse对象
HttpServletResponse对象代表服务器的响应。这个对象中封装了响应客户端浏览器
的流对象，以及向客户端浏览器响应的响应头、响应数据、响应状态码等信息。*/

/*字符响应类型:
设置响应类型为文本型，内容含有html字符串，是默认的响应类型
* resp.setContentType("text/html")
*
设置响应类型为文本型，内容是普通文本
* resp.setContentType("text/plain")

设置响应类型为JSON格式的字符串
resp.setContentType("application/jaon")
*/
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ResponseCharacterServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应类型,要在resp.getWriter();之前
        //resp.setContentType("text/html");
        resp.setContentType("text/plain");
        PrintWriter pw =  resp.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=en>");
        pw.println("<head>");
        pw.println("<meta charset=UTF-8>");
        pw.println("<title>Document</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<font color=blue>HelloWorld</font>");
        pw.println("</body>");
        pw.println("</html>");
        pw.flush();
        pw.close();
    }
}
/*设置响应类型
resp.setContentType("MIME")

该方法可通过MIME-Type设置响应类型。

Type	Meaning
application/msword	             Microsoft Word document
application/octet-stream	    Unrecognized or binary data
application/pdf	                Acrobat (.pdf) file
application/postscript	        PostScript file
application/vnd.lotus-notes	    Lotus Notes file
application/vnd.ms-excel	    Excel spreadsheet
application/vnd.ms-powerpoint	PowerPoint presentation
application/x-gzip	            Gzip archive
application/x-java-archive	    JAR file
application/x-java-serialized-object	Serialized Java object
application/x-java-vm	        Java bytecode (.class) file
application/zip	                Zip archive
application/json	            JSON
audio/basic	                    Sound file in .au or .snd format
audio/midi	                    MIDI sound file
audio/x-aiff	                AIFF sound file
audio/x-wav	Microsoft           Windows sound file
image/gif	                    GIF image
image/jpeg	                    JPEG image
image/png	                    PNG image
image/tiff	                    TIFF image
image/x-xbitmap	                 X Windows bitmap image
text/css	                    HTML cascading style sheet
text/html	                    HTML document
text/plain	                      Plain text
text/xml	                    XML
video/mpeg	                    MPEG video clip
video/quicktime	                QuickTime video clip*/